const formatTime = date => {
  var date = new Date(date);  
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}
const getDate = date => {
  var timerToday = new Date().getTime();
  var dayNum = Math.floor((timerToday - date) / 1000 / 3600 / 24 / 365);
  var dateA = new Date(date);
  const year = dateA.getFullYear()
  const month = dateA.getMonth() + 1
  const day = dateA.getDate()
  const hour = dateA.getHours()
  const minute = dateA.getMinutes()
  var str='';
  if (dayNum>0){
    str = [year, month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
  }else{
    str = [month, day].map(formatNumber).join('-') + ' ' + [hour, minute].map(formatNumber).join(':')
  }
  

  return str
}
const formatDate = date => {
  var date = new Date(date);
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return [year, month, day].map(formatNumber).join('-')
}
const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}
const getDays = date => {
  var timerToday = new Date().getTime();
  var dayNum = Math.floor((timerToday - date) / 1000 / 3600 / 24);
  
  if (dayNum<1){
    var ndate = new Date(date); 
    const hour = ndate.getHours();
    const minute = ndate.getMinutes();
    dayNum = [hour, minute].map(formatNumber).join(':');
  }else if (dayNum >7) {
    dayNum = 7+'天前';
  }else{
    dayNum = dayNum + '天前';
  }
  return dayNum;
}
const getHours = date => {
  var timerToday = new Date().getTime();
  var dayNum = Math.floor((timerToday - date) / 1000 / 3600 / 24);
  if (dayNum < 1) {
    dayNum = Math.floor((timerToday - date) / 1000 / 60 / 60);
    if (dayNum<1){
      dayNum = Math.floor((timerToday - date) / 1000 / 60 );
      if (dayNum < 1){
        dayNum='刚刚';
      }else{
        dayNum = dayNum + '分钟前';
      }
    }else{
      dayNum = dayNum+ '小时前';
    }
  } else if (dayNum > 7) {
    dayNum = 7 + '天前';
  } else {
    dayNum = dayNum + '天前';
  }
  return dayNum;
}
const getNow= function(){
  var date = new Date();
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours();
  return [year, month, day].map(formatNumber).join('-')
}
const verification=function(data,key) {
  var that=this;
  var flag=true;
  for (var i = 0; i < data.length;i++){
    if (data[i].reg){
      var reg = new RegExp(data[i].reg);
      if (!reg.test(that.data[key][data[i].key])) {
        flag = false;
        this.setData({
          'modelData.showModal': true,
          'modelData.content': '请输入正确的' + data[i].label
        })
        return false;
      }
    }
  }
  return flag;
}
module.exports = {
  formatTime: formatTime,
  getDays: getDays,
  getNow: getNow,
  getHours: getHours,
  formatDate: formatDate,
  verification: verification,
  getDate: getDate
}
